/**
 * Copyright (C) 2008-2010 Cristian Olaru <colaru@gmail.com>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *         http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.integrationpath.mengine.blogs;

import javax.servlet.Servlet;


import org.eclipse.equinox.jsp.jasper.JspServlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.util.tracker.ServiceTracker;

import com.integrationpath.mengine.blogs.service.impl.BlogsServiceImpl;
import com.integrationpath.mengine.console.helper.BundleEntryHttpContext;
import com.integrationpath.mengine.console.helper.ContextPathServletAdaptor;
import com.integrationpath.mengine.console.service.PluginService;

/**
 * @author bciurdea@gmail.com
 */
public class Activator implements BundleActivator {

	private ServiceTracker httpServiceTracker;

	public void start(BundleContext context) throws Exception {
		this.httpServiceTracker = new HttpServiceTracker(context);
	    this.httpServiceTracker.open();

	    PluginService service = new BlogsServiceImpl();

	    context.registerService(PluginService.class.getName(), service, null);
	    System.out.println("BLOGS widget registered.");
	}

	public void stop(BundleContext context) throws Exception {
		httpServiceTracker.open();
		System.out.println("BLOGS widget unregistered.");
	}

	private class HttpServiceTracker extends ServiceTracker {

		public HttpServiceTracker(BundleContext context) {
			super(context, HttpService.class.getName(), null);
		}

		public Object addingService(ServiceReference reference) {
			final HttpService httpService = (HttpService) context.getService(reference);
			try {
				HttpContext commonContext = new BundleEntryHttpContext(this.context.getBundle(), "/web");
		        httpService.registerResources("/blogs", "/", commonContext);

		        Servlet adaptedJspServlet = new ContextPathServletAdaptor(new JspServlet(this.context.getBundle(), "/web"), "/blogs");
		        httpService.registerServlet("/blogs/*.jsp", adaptedJspServlet, null, commonContext);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return httpService;
		}

		public void removedService(ServiceReference reference, Object service) {
			final HttpService httpService = (HttpService) service;
			httpService.unregister("/blogs");
			httpService.unregister("/blogs/*.jsp");
			super.removedService(reference, service);
		}			
	}
}


